package org.xq.shop.maxaishop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xq.shop.maxaishop.entity.XqSpkie;
import org.xq.shop.maxaishop.service.XqCommodityService;
import org.xq.shop.maxaishop.service.XqSpkieService;
import org.xq.shop.maxaishop.util.JsonRes;
import org.xq.shop.maxaishop.util.Result;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (XqSpkie)表控制层
 *
 * @author makejava
 * @since 2021-06-19 11:28:30
 */
@Api( tags = "秒杀商品管理")
@RestController
@RequestMapping("xqSpkie")
public class XqSpkieController {
    /**
     * 服务对象
     */
    @Resource
    private XqSpkieService xqSpkieService;
    @Resource
    private XqCommodityService xqCommodityService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据主键查询单个系统用户")
    @ApiImplicitParam(name = "id", value = "秒杀ID", required=true,paramType="query" )
    @PostMapping ("selectOne")
    public XqSpkie selectOne(Integer id) {
        return this.xqSpkieService.queryById(id);
    }

    /**
     * 添加秒杀商品
     * @param xqSpkie
     * @return
     */
    @ApiOperation("发布秒杀")
    //@ApiImplicitParam(name="xqSpkie",value="秒杀对象",required=true,paramType="form")
    @PostMapping("insert")
    public Map insert(XqSpkie xqSpkie){
       xqCommodityService.uopdateStork(xqSpkie.getSpkieLimit(),xqSpkie.getCid());
//        System.out.println(xqSpkie.getSpkieLimit());
       xqSpkie.setXg(xqSpkie.getSpkieLimit());
        System.out.println(xqSpkie);
        XqSpkie insert = this.xqSpkieService.insert(xqSpkie);
        if (insert==null){
          return   JsonRes.failure(null);
        }
        return JsonRes.success(insert);

    }

    /**
     * 秒杀商品
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("秒杀商品展示")
    @PostMapping("selectAll")
    public Result<XqSpkie> select(Integer page,Integer limit){
        int start = (page-1)*limit;
        Result<XqSpkie> result = new Result<XqSpkie>();
        long nowtime = System.currentTimeMillis();

        List<XqSpkie> xqSpkie = this.xqSpkieService.selectAll(start, limit);
        for (XqSpkie x:
             xqSpkie) {
            String time = x.getSpkieEnd()+" "+x.getSpkieEnddate();
            String stime = x.getSpkieBegin()+" "+x.getSpkieBegindate();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = sf.parse(time);
                Date pars = sf.parse(stime);

                 if (nowtime- parse.getTime()>0){
                    x.setMsStatus(0);
                 }else if (nowtime- pars.getTime()<0){
                     x.setMsStatus(2);
                 }else {
                     x.setMsStatus(1);
                 }

            } catch (ParseException e) {
                e.printStackTrace();
            }


        }
        int count = this.xqSpkieService.selectCount();
        result.setCode(200);
        result.setCount(count);
        result.setMsg("请求成功");
        result.setData(xqSpkie);
        return result;
    }

    /***
     * 今日秒杀
     * @param t
     * @return
     */
    @ApiOperation("今日秒杀商品展示")
    @PostMapping("selectToDay")
    public List<XqSpkie>  selectToDay(String t){
        System.out.println(t);
        Date date = new Date();
//        XqSpkie xqSpkie = new XqSpkie();
//        System.out.println(date.getTime());
//        xqSpkie.setNowTime();
        List<XqSpkie> xqSpkies = this.xqSpkieService.selectToDay(t);
        for (XqSpkie x:
                xqSpkies ) {
          x.setNowTime(System.currentTimeMillis());

        }
        return xqSpkies;
    }



    /**
     * 秒杀信息
     * @param mid
     * @return
     */

    @ApiOperation("根据主键查询单个秒杀商品")
    @ApiImplicitParam(name = "mid", value = "秒杀ID", required=true,paramType="query" )
    @PostMapping("selectMs")
    public XqSpkie selectMs(Integer mid){
        XqSpkie xqSpkie = this.xqSpkieService.selectMs(mid);
        xqSpkie.setNowTime(System.currentTimeMillis());
        return xqSpkie;
    }

    /**
     * 更新库存
     * @param aid
     * @return
     */
    @GetMapping("updateSales")
    public int updateSales(int aid) {
        return this.xqSpkieService.updateSales(aid);
    }
}
